package com.dqyt.app.service;

import com.dqyt.app.bean.UserInfo;
import com.dqyt.common.check.CheckTool;
import com.dqyt.common.constant.AuthorityID;
import com.dqyt.common.constant.Opt;
import com.dqyt.common.utils.RegularUtil;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.CustomerBean;
import com.dqyt.khjc.bean.HistoryBean;
import com.dqyt.khjc.bean.ResultMsg;
import com.dqyt.khjc.bean.User;
import com.dqyt.khjc.mapper.CustomerMapper;
import com.dqyt.khjc.mapper.HistoryMapper;
import com.dqyt.khjc.utils.IpUtil;
import com.dqyt.khjc.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Service
public class AppCustomerService {

    @Autowired
    private CustomerMapper customerMapper;


    @Autowired
    private HistoryMapper historyMapper;

    public ResultMsg getypglCustomerList(HttpServletRequest request, long userId,String token){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();

            ret.setCode(ResultMsg.SUCESS);
            List<CustomerBean> customerBeans = customerMapper.getCustomerList();
            if (null != customerBeans) {
                ret.setData(customerBeans);
            } else {
                ret.setData(new ArrayList<CustomerBean>());
            }

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 查询某个单位的 下属单位列表
     * @param request
     * @param userId
     * @param token
     * @return
     */
    public ResultMsg getypglCustomerChiList(HttpServletRequest request, long userId,String token,int customerId){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();
            ret.setCode(ResultMsg.SUCESS);
            List<CustomerBean> customerBeans = customerMapper.getXzkhrylCustomerListCha(customerId);
            if (null != customerBeans) {
                ret.setData(customerBeans);
            } else {
                ret.setData(new ArrayList<CustomerBean>());
            }

        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 新增客户信息
     *
     * @param enterpriseName 客户名称
     * @param province       省
     * @param city           市
     * @param block          区
     * @param address        地址
     * @param contacts       联系人
     * @param phone          联系人手机
     * @param email          电子邮箱
     * @param telephone      座机
     * @param fax            传真
     * @return
     */
    public ResultMsg addCustomer(HttpServletRequest request,long userId,String token, String enterpriseName, int province, int city, int block,
                                 String address, String contacts, String phone, String email, String telephone, String fax) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();

            //参数判断
            if (null == enterpriseName ||"".equals(enterpriseName) || enterpriseName.length() > 40||
                    province == 0 || city == 0 || block == 0 ||
                    null == address ) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("非法操作");
                return ret;
            }

            //更具名称查询是否重名
            long mcount =  customerMapper.getcustomerByName(0,enterpriseName);
            if(mcount>0){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("已经存在该企业");
                return ret;
            }


            User  use=  UserInfoUtil.getUserInfologin(userId, token);
            CustomerBean customerBean = new CustomerBean();
            customerBean.setEnterpriseName(enterpriseName);
            customerBean.setProvince(province);
            customerBean.setCity(city);
            customerBean.setBlock(block);
            customerBean.setAddress(address);
            customerBean.setContacts(contacts);
            customerBean.setPhone(phone);
            customerBean.setpName("");
            customerBean.setEmail(email);
            customerBean.setTelephone(telephone);
            customerBean.setExamineType(1);
            customerBean.setFax(fax);
            customerBean.setAddTime(TimeUtil.currentTimeMillis());
            customerBean.setAddUser(String.valueOf(userId));
            customerBean.setUpdateTime(TimeUtil.currentTimeMillis());
            customerBean.setUpdateUser(String.valueOf(userId));
            int res = customerMapper.addCustomer(customerBean);
            if (res <= 0) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("数据异常");
                return ret;
            }
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("添加成功");
            //添加操作记录
            // 先取得用户ip
            String ip = IpUtil.getIpAddr(request);
            HistoryBean historyBean = new HistoryBean();
            historyBean.setMessage("[" + use.getUserName() + "] 新增了一条客户名称为： [" + enterpriseName + "] 的客户信息");
            historyBean.setType(Opt.KHGL_XZKHXX_XZ);
            historyBean.setOpUserId(userId);
            historyBean.setIp(ip);
            historyBean.setBopId(String.valueOf(customerBean.getCustomerId()));
            historyBean.setAddTime(TimeUtil.currentTimeMillis());
            historyBean.setAddUser(String.valueOf(userId));
            historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
            historyBean.setUpdateUser(String.valueOf(userId));
            //插入数据库
            int ress = historyMapper.addHistory(historyBean);

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


}
